package zjy.android.radapter;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import zjy.android.radapter.adapter.factory.LiveDataCallAdapterFactory;
import zjy.android.radapter.converter.factory.ReturnConverterFactory;
import zjy.android.radapter.exception.HttpReturnException;
import zjy.android.radapter.livedata.ReturnObserver;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        new Retrofit.Builder()
//                .baseUrl("http://alliance.yunzhanxinxi.com/")
//                .addConverterFactory(ReturnConverterFactory.create(HttpResult.class, true))
//                .addConverterFactory(GsonConverterFactory.create())
//                .client(client)
//                .build()
//                .create(LoginService.class)
//                .login1("13192294682", "76913ff950d9c25754217451fdaf54db")
//                .enqueue(new Callback<HttpResult<LoginInfo>>() {
//                    @Override
//                    public void onResponse(@NonNull Call<HttpResult<LoginInfo>> call,
//                                           @NonNull Response<HttpResult<LoginInfo>> response) {
//                        if (response.body() != null) {
//                            Log.e("TAG", "onResponse: " + response.body().data.userName);
//                        }
//                        Log.e("TAG", "onResponse: ");
//                    }
//
//                    @Override
//                    public void onFailure(@NonNull Call<HttpResult<LoginInfo>> call, @NonNull Throwable t) {
//                        Log.e("TAG", "onFailure: " + t.toString());
//                    }
//                });

        login("1319229468");
    }

    private void login(String name) {
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new HttpLoggingInterceptor(message -> Log.e("TAG",
                        "log: " + message)).setLevel(HttpLoggingInterceptor.Level.BODY)).build();
        new Retrofit.Builder()
                .baseUrl("http://alliance.yunzhanxinxi.com/")
                .addConverterFactory(ReturnConverterFactory.create(HttpResult.class, true))
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(new LiveDataCallAdapterFactory())
                .client(client)
                .build()
                .create(LoginService.class)
                .login(name, "76913ff950d9c25754217451fdaf54db")
                .observe(this, new ReturnObserver<LoginInfo>() {
                    @Override
                    public void onFailure(Throwable throwable) {
                        if (throwable instanceof HttpReturnException) {
                            HttpResult<?> result = ((HttpReturnException) throwable).getReturnSource();
                            ((TextView) findViewById(R.id.text)).setText(result.message);
                        } else {
                            ((TextView) findViewById(R.id.text)).setText(throwable.toString());
                        }
                    }

                    @Override
                    public void onChanged(LoginInfo loginInfo) {
                        ((TextView) findViewById(R.id.text)).setText(loginInfo.userName);
                    }
                });
    }

    @Override
    protected void onStop() {
        super.onStop();
        login("13192294682");
    }
}